//
//  DetailPersonalViewController.m
//  Contractor
//
//  Created by 金鑫 on 16/5/4.
//  Copyright © 2016年 star j. All rights reserved.
//

#import "DetailPersonalViewController.h"
#import "DetailTableViewCell.h"
#import "reviseViewController.h"
#import "CONST.h"

@interface DetailPersonalViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIScrollView *ScrollView ;
    UITableView *UpTableView ;
    UITableView *DownTableView ;
    UIView *headBackView ;
    ModelPersonal *detailmodel ;
    NSString *liveLocationStr ;
}

@end

@implementation DetailPersonalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    ScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame] ;
    ScrollView.backgroundColor = [UIColor lightGrayColor] ;
    
    NSLog(@"%f,%f",[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height) ;
    ScrollView.showsVerticalScrollIndicator = NO ;
    ScrollView.bounces = NO ;
    [self.view addSubview:ScrollView] ;
    //[self createTableView] ;
}


#pragma mark - 创建头像
- (void)creatHeadPic
{
    //背景View
    CGFloat height = __kScreenHeight / 4 ;
    headBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, __kScreenWidth, height)] ;
    headBackView.backgroundColor = [UIColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:1] ;
    [ScrollView addSubview:headBackView] ;
    
    //头像图标
    CGFloat btnSize = __kScreenWidth / 5 ;
    UIImageView *headView = [[UIImageView alloc] initWithFrame:CGRectMake((__kScreenWidth - btnSize) / 2, (height - btnSize) / 2-10, btnSize, btnSize)] ;
    headView.layer.cornerRadius = btnSize / 2 ;
    [headView setImage:[UIImage imageNamed:@"default_user_icon.png"]] ;
    [headBackView addSubview:headView] ;
    
    //姓名
    NSLog(@"realname2:%@",detailmodel.realname) ;
    UILabel *nameLabel = [[UILabel alloc] init] ;
    float labelY = headView.frame.origin.y + btnSize + 10 ;
    
    nameLabel.font = [UIFont fontWithName:@"AvenirLTStd-Light" size:14.0f] ;
    nameLabel.backgroundColor = [UIColor clearColor] ;
    NSString *gender = nil ;
    if (detailmodel.gender == 1) {
        gender = @"男" ;
    }
    else
    {
        gender = @"女" ;
    }
    nameLabel.text = [NSString stringWithFormat:@"%@  %@",detailmodel.realname,gender] ;
    //CGSize nameSize = [self lengthwithLabel:nameLabel withsize:14.0f] ;
    nameLabel.frame = CGRectMake(0, labelY, __kScreenWidth, 16.0f) ;
    nameLabel.textAlignment = NSTextAlignmentCenter ;
    [headBackView addSubview:nameLabel] ;
    
    [self createTableView] ;
}

#pragma mark - 创建tableview
- (void)createTableView
{
    UpTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, __kScreenHeight / 4 + 0.5, __kScreenWidth, 149.5) style:UITableViewStylePlain] ;
    UpTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine ;
    UpTableView.showsVerticalScrollIndicator = NO ;
    UpTableView.scrollEnabled = NO ;
    UpTableView.delegate = self ;
    UpTableView.dataSource = self ;
    [ScrollView addSubview:UpTableView] ;
    
    DownTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, __kScreenHeight / 4 + 150 + 8, __kScreenWidth, 199.5) style:UITableViewStylePlain] ;
    DownTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine ;
    DownTableView.showsVerticalScrollIndicator = NO ;
    DownTableView.scrollEnabled = NO ;
    DownTableView.delegate = self ;
    DownTableView.dataSource = self ;
    [ScrollView addSubview:DownTableView] ;
    
    CGFloat scrollViewY = DownTableView.frame.origin.y + DownTableView.frame.size.height ;
    ScrollView.contentSize = CGSizeMake(0, scrollViewY + 150) ;
    
    UIView *reviseView = [[UIView alloc] initWithFrame:CGRectMake(0, scrollViewY+8, __kScreenWidth, 142)] ;
    reviseView.backgroundColor = [UIColor whiteColor] ;
    [ScrollView addSubview:reviseView] ;
    
    UIButton *reviseBtn = [UIButton buttonWithType:UIButtonTypeCustom] ;
    reviseBtn.frame = CGRectMake(20, 18, __kScreenWidth - 40, 40) ;
    [reviseBtn setTitle:@"账户修改" forState:UIControlStateNormal] ;
    reviseBtn.backgroundColor = [UIColor blueColor] ;
    reviseBtn.layer.cornerRadius = 10.0f ;
    [reviseBtn addTarget:self action:@selector(revise) forControlEvents:UIControlEventTouchUpInside] ;
    [reviseView addSubview:reviseBtn] ;

}

- (void)revise
{
    reviseViewController *reviseView = [[reviseViewController alloc] init] ;
    reviseView.addressStr = liveLocationStr ;
    reviseView.RzStr = detailmodel.renzheng ;
    reviseView.model = detailmodel ;
    self.hidesBottomBarWhenPushed = YES ;
    [self.navigationController pushViewController:reviseView animated:NO] ;
    //self.hidesBottomBarWhenPushed = YES ;
}

#pragma mark - TableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1 ;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([tableView isEqual: UpTableView]) {
        return 3 ;
    }
    else
    {
        return 4 ;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:UpTableView]) {
        DetailTableViewCell *cell = [[DetailTableViewCell alloc] init] ;
        cell.selectionStyle = UITableViewCellSelectionStyleNone ;
        NSArray *titleArray = @[@"电话",@"身份证",@"绑定验证码"] ;
        UIImage *image1 = [UIImage imageNamed:@"telephone.png"] ;
        UIImage *image2 = [UIImage imageNamed:@"idcard.png"] ;
        UIImage *image3 = [UIImage imageNamed:@"personal_center_bindercode_icon.png"] ;
        NSArray *imageArray = @[image1,image2,image3] ;
        
        NSString *idStr = nil ;
        if (detailmodel.idcard.length < 4) {
            idStr = @"" ;
        }
        else{
        NSString *idfirstStr = [detailmodel.idcard substringToIndex:4] ;
        NSString *idlastStr = [detailmodel.idcard substringFromIndex:14] ;
        idStr = [NSString stringWithFormat:@"%@**********%@",idfirstStr,idlastStr] ;
        }
        
        NSArray *dataArray = @[detailmodel.nickname,idStr,detailmodel.bindingpassword] ;
        [cell configTitle:titleArray[indexPath.row] configImage:imageArray[indexPath.row] withData:dataArray[indexPath.row]] ;
        return cell ;
    }
    else{
        DetailTableViewCell *cell = [[DetailTableViewCell alloc] init] ;
        cell.selectionStyle = UITableViewCellSelectionStyleNone ;
        NSArray *titleArray = @[@"合作团队",@"服务区域",@"所在区域",@"当前位置"] ;
        UIImage *image1 = [UIImage imageNamed:@"partner.png"] ;
        UIImage *image2 = [UIImage imageNamed:@"service_aere.png"] ;
        UIImage *image3 = [UIImage imageNamed:@"location_address.png"] ;
        UIImage *image4 = [UIImage imageNamed:@"location_address.png"] ;
        NSArray *imageArray = @[image1,image2,image3,image4] ;
        
        liveLocationStr = [NSString stringWithFormat:@"%@ %@ %@ %@",detailmodel.liveLocation1,detailmodel.liveLocation2,detailmodel.liveLocation3,detailmodel.liveaddress] ;
        NSLog(@"liveLocationStr:%@",liveLocationStr) ;
        NSString *nowLocation = [[NSString alloc] init] ;
        if (detailmodel.nowLocation) {
            nowLocation = detailmodel.nowLocation ;
        }
        else
        {
            nowLocation = @"" ;
        }
        NSArray *dataArray = @[detailmodel.fid_realname,detailmodel.serviceLocate1,liveLocationStr,nowLocation] ;
        [cell configTitle:titleArray[indexPath.row] configImage:imageArray[indexPath.row] withData:dataArray[indexPath.row]] ;
        return cell ;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50 ;
}

- (void)downloadPersonalDate
{
    NSUserDefaults *uidDefaults = [NSUserDefaults standardUserDefaults] ;
    NSInteger uidInt = [[uidDefaults valueForKey:@"uid"] integerValue] ;
    
    NSNumber *cmd = [[NSNumber alloc] initWithInt:8] ;
    NSNumber *uid = [[NSNumber alloc] initWithInteger:uidInt] ;
    NSDictionary *parameters = @{@"cmd":cmd,@"uid":uid};
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:getAppJson parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         //解密并解析json数据，将json转成字典
         NSString *decodeStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding] ;
         NSLog(@"decodeStr:%@",decodeStr) ;
         decodeStr = [CodeWithDES decodeWithStr:decodeStr] ;
         NSLog(@"decodeStr1:%@",decodeStr) ;
         NSData *decodeData = [decodeStr dataUsingEncoding:NSUTF8StringEncoding] ;
         id result = [NSJSONSerialization JSONObjectWithData:decodeData options:NSJSONReadingMutableContainers error:nil] ;
         //获取字典中的值
         if ([result isKindOfClass:[NSDictionary class]])
         {
             NSDictionary *dic = result ;
             // NSLog(@"dic:%@",dic) ;
             if (([dic[@"status"] integerValue] == 1) && ![dic[@"content"] isKindOfClass:[NSNull class]])
             {
                 for (NSDictionary *content in dic[@"content"])
                 {
                     [detailmodel setValuesForKeysWithDictionary:content] ;
                     detailmodel.nowLocation = [uidDefaults valueForKey:@"location"] ;
                     NSLog(@"realname:%@",detailmodel.realname) ;
                     [self creatHeadPic] ;
                 }
             }
             [UpTableView reloadData] ;
             [DownTableView reloadData] ;
             
         }
     }
          failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         NSLog(@"Error: %@", error);
     }];
}


- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO ;
    self.navigationItem.title = @"个人信息" ;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.edgesForExtendedLayout = UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight;
    detailmodel = [[ModelPersonal alloc] init] ;
    [self downloadPersonalDate] ;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES ;
}

- (void)viewDidAppear:(BOOL)animated
{
    NSLog(@"viewDidAppear+++");
    
    //获取数据
    [self downloadPersonalDate];
}

-(void)viewDidDisappear:(BOOL)animated
{
    NSLog(@"viewDidDisappear+++****");
    [UpTableView removeFromSuperview] ;
    [DownTableView removeFromSuperview] ;
    [headBackView removeFromSuperview] ;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
